#include "HY1C_out.h"
#include "config.h"

int write_log(LogLevel_em logLevel,
              const char *file,
              const char *func,
              const int line,
              const char *format,
              ...)
{
    va_list arg;
    int done;

    if (logLevel < g_cfg.log_level) {
        return 0;
    }

    char* loglevel_str[] = {"DEBUG", "INFO", "WARN", "ERROR"};

    va_start (arg, format);
    time_t time_log = time(NULL);
    struct tm* tm_log = localtime(&time_log);
    fprintf(g_cfg.pLog, "+++ %04d-%02d-%02d %02d:%02d:%02d %s %s %s %d - ",
            tm_log->tm_year + 1900,
            tm_log->tm_mon + 1,
            tm_log->tm_mday,
            tm_log->tm_hour,
            tm_log->tm_min,
            tm_log->tm_sec,
            loglevel_str[logLevel],
            file, func, line);

    //if (logLevel >= LogLevel_Warn) {
    //    vfprintf (stderr, format, arg);
    //}

    done = vfprintf (g_cfg.pLog, format, arg);
    va_end (arg);
    fflush(g_cfg.pLog);
    return done;
}
